﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_UserControl_tin_dang_edit : System.Web.UI.UserControl
{
    protected int iTI_ID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        //Config tollbar
        txtMoTa.config.toolbar = new object[]
            {
               new object[] { "Source", "Bold", "Italic", "Underline", "Strike","JustifyLeft", "JustifyCenter", "JustifyRight", "-", "Subscript", "Superscript", "TextColor", "BGColor" },
            };
        txtDauTu.config.toolbar = new object[]
            {
               new object[] { "Source", "Bold", "Italic", "Underline", "Strike","JustifyLeft", "JustifyCenter", "JustifyRight", "-", "TextColor", "BGColor" },
            };
        txtThoiGian.config.toolbar = new object[]
            {
               new object[] { "Source", "Bold", "Italic", "Underline", "Strike","JustifyLeft", "JustifyCenter", "JustifyRight", "-", "TextColor", "BGColor" },
            };

        iTI_ID = OsPortal.oStringHelper.fncCnvNullToInt(clsCommon.getParameter(basConst.QR_URL_ID));
        if (!IsPostBack)
        {
            xGetChuyenMucChinh();
            xGetChuyenMucTree();
            xGetAuthor();
            txtNgayBatDau.Text = String.Format("{0:dd/MM/yyyy}",DateTime.Now);
            txtNgayKetThuc.Text = String.Format("{0:dd/MM/yyyy}",DateTime.Now);
            if (iTI_ID != 0)
            {
                xGetDetailsTI();
            }
        }
    }
    /// <summary>
    /// Lấy danh sách chuyên mục chính
    /// </summary>
    private void xGetChuyenMucChinh()
    {
        ddlChuyenMuc.DataSource = clsDanhMuc.fncGetByParentID(0);
        ddlChuyenMuc.DataTextField = "dm_tieude";
        ddlChuyenMuc.DataValueField = "DM_ID";
        ddlChuyenMuc.DataBind();
    }
    /// <summary>
    /// Lấy cây danh mục
    /// </summary>
    private void xGetChuyenMucTree()
    {
        ddlChuyenMucLienQuan.DataSource = clsDanhMuc.fncGetTree();
        ddlChuyenMucLienQuan.DataTextField = "TenDanhMuc";
        ddlChuyenMucLienQuan.DataValueField = "DanhMucID";
        ddlChuyenMucLienQuan.DataBind();
    }
    /// <summary>
    /// Lấy danh sách diễn giả
    /// </summary>
    private void xGetAuthor()
    {
        ddlAuthor.DataSource = clsAuthor.fncGetAll();
        ddlAuthor.DataTextField = "at_name";
        ddlAuthor.DataValueField = "AT_ID";
        ddlAuthor.DataBind();
    }

    private void xGetDetailsTI()
    {
        DataTable dt = clsTinDang.fncTinDang_FND(1,1,"",iTI_ID,"","",-1);
        txtTieuDe.Text = dt.Rows[0]["TI_TIEUDE"].ToString();
        txtMoTa.Text = dt.Rows[0]["TI_MOTA"].ToString();
        txtDauTu.Text = dt.Rows[0]["TI_DAUTU"].ToString();
        txtThoiGian.Text = dt.Rows[0]["TI_THOIGIAN"].ToString();
        //txtNoiDung.Text = System.Net.WebUtility.HtmlDecode(dt.Rows[0]["TI_NOIDUNG"].ToString());
        CKEditor1.Text = System.Net.WebUtility.HtmlDecode(dt.Rows[0]["TI_NOIDUNG"].ToString());
        txtNgayBatDau.Text = String.Format("{0:dd/MM/yyyy}",Convert.ToDateTime(dt.Rows[0]["TI_NGAY_BATDAU"]));
        txtNgayKetThuc.Text = String.Format("{0:dd/MM/yyyy}",Convert.ToDateTime(dt.Rows[0]["TI_NGAY_KETTHUC"]));
        hdAnh1.Value = dt.Rows[0]["TI_ANH"].ToString();
        string status = dt.Rows[0]["TI_STATUS"].ToString();
        if (status == "True"){rbtShow.Checked = true;}else {rbtHide.Checked = true; }
        ddlChuyenMuc.SelectedValue = dt.Rows[0]["DM_ID"].ToString();
        ddlChuyenMucLienQuan.SelectedValue = dt.Rows[0]["DM_LIENQUAN"].ToString();
        ddlAuthor.SelectedValue = dt.Rows[0]["AT_ID"].ToString();
        ddl_kieutin.SelectedValue = dt.Rows[0]["TI_KIEUTIN"].ToString();
    }

    /// <summary>
    /// Lay gia tri tren form
    /// </summary>
    /// <returns></returns>
    private clsTinDang.stTinDang xGetValueForm()
    {
        int widthImages = Convert.ToInt16(OsPortal.oStringHelper.fncGetAppSettings("DO_RONG_ANH_THUMBNAIL"));

        clsTinDang.stTinDang objTinDang = new clsTinDang.stTinDang();
        objTinDang.AT_ID = OsPortal.oStringHelper.fncCnvNullToInt(ddlAuthor.SelectedValue);
        objTinDang.DM_ID = OsPortal.oStringHelper.fncCnvNullToInt(ddlChuyenMuc.SelectedValue);
        objTinDang.DM_LIENQUAN = OsPortal.oStringHelper.fncCnvNullToInt(ddlChuyenMucLienQuan.SelectedValue);
        objTinDang.TI_ID = iTI_ID;
        objTinDang.TI_TIEUDE = txtTieuDe.Text.Trim();
        objTinDang.TI_MOTA = txtMoTa.Text.Trim();
        objTinDang.TI_DAUTU = txtDauTu.Text.Trim();
        objTinDang.TI_THOIGIAN = txtThoiGian.Text.Trim();
        objTinDang.TI_KIEUTIN = OsPortal.oStringHelper.fncCnvNullToInt(ddl_kieutin.SelectedValue);
        //
        string strNgayBatDau = txtNgayBatDau.Text.Trim();
        DateTime dtStartDate = DateTime.Now;
        if (!string.IsNullOrEmpty(strNgayBatDau))
        {
            string strStartDate = strNgayBatDau + " " + DateTime.Now.ToString("HH:mm:ss");
            IFormatProvider culture = new CultureInfo("vi-VN", true);
            dtStartDate = DateTime.ParseExact(strStartDate, "dd/MM/yyyy HH:mm:ss", culture);
        } 
        objTinDang.TI_NGAY_BATDAU = dtStartDate;
        //
        string strNgayKetThuc = txtNgayKetThuc.Text.Trim();
        DateTime dtEndDate = DateTime.Now;
        if (!string.IsNullOrEmpty(strNgayKetThuc))
        {
            string strEndDate = strNgayKetThuc + " " + DateTime.Now.ToString("HH:mm:ss");
            IFormatProvider culture = new CultureInfo("vi-VN", true);
            dtEndDate = DateTime.ParseExact(strEndDate, "dd/MM/yyyy HH:mm:ss", culture);
        }
        objTinDang.TI_NGAY_KETTHUC = dtEndDate;
        //
        objTinDang.TI_NOIDUNG = CKEditor1.Text;//Server.HtmlDecode(txtNoiDung.Text);

        int iDisplay = 1;
        if (rbtHide.Checked) iDisplay = 0;

        objTinDang.TI_STATUS = iDisplay;


        //Ảnh 1
        string strAnh1 = hdAnh1.Value;
        if (fulAnh1.HasFile)
        {
            string strOutImagesName;
            OsPortal.oFileHelper.UploadAnhResizeImage(fulAnh1, basConst.PATH_IMAGES_USER, widthImages, out strOutImagesName, "", false);
            if (!string.IsNullOrEmpty(strOutImagesName))
                strAnh1 = basConst.PATH_IMAGES_USER.Replace("~/", "") + strOutImagesName;

        }
        objTinDang.TI_ANH = strAnh1;

        return objTinDang;
    }

    /// <summary>
    /// Su kien click
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //clsTinDang.stTinDang objTinDang = xGetValueForm();
        if (iTI_ID != 0)
        {
            string strResult = clsTinDang.fncTinDang_Act(xGetValueForm(), basConst.MOD_UPDATE);
            if (strResult == "")
            {
                OsPortal.oHtmlHelper.AlertAndRederect("Cập nhật thành công", "/admin/default.aspx?mo=tin-dang-list", this.Page);
            }
            else
            {
                OsPortal.oHtmlHelper.Alert(strResult, this.Page);
            }
        }
        else
        {
            string strResult = clsTinDang.fncTinDang_Act(xGetValueForm(), basConst.MOD_ADD);
            if (strResult == "")
            {
                OsPortal.oHtmlHelper.AlertAndRederect("Thêm mới thành công", "/admin/default.aspx?mo=tin-dang-list", this.Page);
            }
            else
            {
                OsPortal.oHtmlHelper.Alert(strResult, this.Page);
            }
        }
    }
}